import { createRouter, createWebHistory } from "vue-router";
import Index from "../pages/Index.vue";
import Love from "../pages/Love.vue";

// 项目其他页面路由（推荐使用）
// import Practice from "../router/practiceFolder/practice";
// vue项目自带路由
// const routes: Array<RouteRecordRaw> = [
const routes = [
  {
    path: "/",
    name: "index",
    component: Index,
  },
  // {
  //   path: "/love",
  //   name: "love",
  //   // redirect: "/Love",
  //   component: Love,
  // },
  // {
  //   path: "/news",
  //   name: "News",
  //   component: () => import("../pages/News.vue"),
  //   redirect: "/news/description",
  //   children: [
  //     {
  //       path: "description",
  //       name: "description",
  //       component: () =>
  //         import(
  //           /* webpackChunkName: "news" */ "../pages/news/Description.vue"
  //         ),
  //     },
  //     {
  //       path: "description/:id?/:name?/:content?",
  //       name: "description",
  //       component: () =>
  //         import(
  //           /* webpackChunkName: "news" */ "../pages/news/Description.vue"
  //         ),
  //       props(r: any) {
  //         const { params, query } = r;
  //         console.log("r---", r);
  //         if (Object.keys(params)) return params;
  //         if (Object.keys(query)) return query;
  //       },
  //     },
  //   ],
  // },
  // {
  //   path: "/sub",
  //   name: "sub",
  //   component: () => import("../pages/Sub.vue"),
  //   redirect: "/sub/prop",
  //   children: [
  //     {
  //       path: "prop",
  //       name: "prop",
  //       component: () => import("../pages/sub/prop.vue"),
  //     },
  //     {
  //       path: "emit",
  //       name: "emit",
  //       component: () => import("../pages/sub/emit.vue"),
  //     },
  //     {
  //       path: "mitt",
  //       name: "mitt",
  //       component: () => import("../pages/sub/mitt.vue"),
  //     },
  //     {
  //       path: "model",
  //       name: "model",
  //       component: () => import("../pages/sub/model.vue"),
  //     },
  //     {
  //       path: "attrs",
  //       name: "attrs",
  //       component: () => import("../pages/sub/attrs.vue"),
  //     },
  //     {
  //       path: "refsAndParent",
  //       name: "refsAndParent",
  //       component: () => import("../pages/sub/refsAndParent.vue"),
  //     },
  //     {
  //       path: "provideInject",
  //       name: "provideInject",
  //       component: () => import("../pages/sub/provide-inject.vue"),
  //     },
  //     {
  //       path: "slots",
  //       name: "slots",
  //       component: () => import("../pages/sub/slots.vue"),
  //     },
  //   ],
  // },
  // {
  //   path: "/shallow",
  //   name: "shallow",
  //   // redirect: "/Love",
  //   component: import("../pages/Shallow.vue"),
  // },
  // {
  //   path: "/teleport",
  //   name: "teleport",
  //   // redirect: "/Love",
  //   component: import("../pages/Teleport.vue"),
  // },
  // {
  //   path: "/suspense",
  //   name: "suspense",
  //   // redirect: "/Love",
  //   component: import("../pages/Suspense.vue"),
  // },
];
// const routers = [...routes, ...Practice];
const router = createRouter({
  // history: createWebHistory(process.env.BASE_URL),
  history: createWebHistory(),
  // routes: [...routes, ...Practice]
  routes: [...routes],
});

const views = import.meta.glob("../pages/**/**.vue");

Object.keys(views).forEach((fileName) => {
  if (fileName) {
    const match = fileName.match(/\.\/pages\/(.+)\.vue$/);
    // console.log("name", fileName, match);
    if (match && match[1]) {
      const name = match[1];
      console.log("name.toLowerCase()", name.toLowerCase());
      router.addRoute({
        path: `/${name.toLowerCase()}`,
        component: () => views[fileName](),
        meta: {
          title: name,
        },
      });
    } else {
      // 处理文件名不匹配的情况，可能需要记录日志或抛出错误
      console.error(`文件名 ${fileName} 不匹配预期的格式`);
    }
  }
});
console.log("router", router.getRoutes());
export default router;
